//
//  HLIMInputBar.m
//  YueYue
//
//  Created by hulu001 on 2023/6/13.
//

#import "HLIMInputBar.h"

@interface HLIMInputBar ()

@property (strong,  nonatomic) id<HLIMKitInputView> inputView;
@property (nonatomic, strong) QMUIFloatLayoutView *tabsView;

@end

@implementation HLIMInputBar

- (instancetype)initWithFrame:(CGRect)frame delegate:(id<HLIMKitInputBarDelegate>)delegate
{
    self = [super initWithFrame:frame delegate:delegate];
    if (self) {
        
    }
    return self;
}

- (void)addCustomButton
{
    CGFloat itemSize = 42;
    CGFloat spacing = (self.qmui_width - 5 * itemSize) / 6.f;
    self.tabsView = [[QMUIFloatLayoutView alloc] init];
    //_labelsView.padding = UIEdgeInsetsMake(0, 4, 0, 4);
    _tabsView.itemMargins = UIEdgeInsetsMake(0, spacing/2, 0, spacing/2);
    _tabsView.minimumItemSize = CGSizeMake(itemSize, itemSize);// 以2个字的按钮作为最小宽度
    [self addSubview:_tabsView];
    [_tabsView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.height.mas_equalTo(42);
        make.left.mas_equalTo(spacing);
        make.right.mas_equalTo(-spacing);
        make.bottom.mas_equalTo(0);
    }];
    
    
    // 表情
    UIButton *emojiButton = [self barButton:[UIImage imageNamed:@"imEmoji"]];
    [_tabsView addSubview:emojiButton];
    self.emojiButton = emojiButton;
    
    // 图片
    UIButton *photoButton = [self barButton:[UIImage imageNamed:@"imAblum"]];
    [_tabsView addSubview:photoButton];
    self.photoButton = photoButton;
    
    // 视频通话
    UIButton *callButton = [self barButton:[UIImage imageNamed:@"imVideo"]];
    [_tabsView addSubview:callButton];
    self.callButton = callButton;
    
    // 礼物
    UIButton *giftButton = [self barButton:[UIImage imageNamed: [HLRunTime shareInstance].isFood? @"imAudioCall": @"imGift"]];
    [_tabsView addSubview:giftButton];
    self.giftButton = giftButton;

    
    UIButton *realVoiceButton = [self barButton:[UIImage imageNamed:@"imAudioRecording"]];
    [_tabsView addSubview:realVoiceButton];
    self.realVoiceButton = realVoiceButton;

    
    
//    UIButton *topicBtn = [self barButton:[UIImage imageNamed:@"imTopic"]];
//    [_tabsView addSubview:topicBtn];
//    self.topicButton = topicBtn;
    
    
    // 使用方法 2，以 UIWindow 的形式显示到界面上，这种无需默认隐藏，也无需 add 到某个 UIView 上
//    self.popupByWindow = [[QMUIPopupMenuView alloc] init];
//    self.popupByWindow.automaticallyHidesWhenUserTap = YES;// 点击空白地方消失浮层
//    //self.popupByWindow.tintColor = UIColorWhite;
//    self.popupByWindow.backgroundColor = UIColorWhite;
//    self.popupByWindow.itemTitleColor = HEXColor(0x1F1F1F);
//    self.popupByWindow.itemSeparatorColor = HEXColor(0xF5F6F8);
//    self.popupByWindow.padding = UIEdgeInsetsMake(10, 28, 10, 28);
//    self.popupByWindow.itemTitleFont = [UIFont systemFontOfSize:12 weight:UIFontWeightMedium];
//    self.popupByWindow.maskViewBackgroundColor = [UIColorWhite colorWithAlphaComponent:.15];// 使用方法 2 并且打开了 automaticallyHidesWhenUserTap 的情况下，可以修改背景遮罩的颜色
//    self.popupByWindow.shouldShowItemSeparator = YES;
//    [self updatePriceWithVideo:@"0" audio:@"0"];
//    self.popupByWindow.sourceView = self.callButton;// 相对于 button4 布局
//    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(inviteAction) name:kInvitorVideoOrAudioNotiKey object:nil];
}

- (void)addCustomTarget:(id<HLIMKitInputView>)target
{
    self.inputView = target;

    [self.giftButton addTarget:self action:@selector(sendGift:) forControlEvents:UIControlEventTouchUpInside];
    [self.photoButton addTarget:self action:@selector(tapPhoto) forControlEvents:UIControlEventTouchUpInside];
    [self.callButton addTarget:self action:@selector(videoCall) forControlEvents:UIControlEventTouchUpInside];
    [self.audioButton addTarget:self action:@selector(audioCall) forControlEvents:UIControlEventTouchUpInside];
    [self.topicButton addTarget:self action:@selector(topicEvent:) forControlEvents:UIControlEventTouchUpInside];
}

- (void)sendGift:(id)sender
{
    [self.inputView closeKeyboard];
    if ([self.inputView.actionDelegate respondsToSelector:@selector(onTapGiftButton:)]) {
        [self.inputView.actionDelegate onTapGiftButton:sender];
    }
}

- (void)tapPhoto
{
    [self.inputView closeKeyboard];
    if ([self.inputView.actionDelegate respondsToSelector:@selector(onTapPhotoButton)]) {
        [self.inputView.actionDelegate onTapPhotoButton];
    }
}

- (void)videoCall
{
    [self.inputView closeKeyboard];
    if ([self.inputView.actionDelegate respondsToSelector:@selector(onTapVideoCall)]) {
        [self.inputView.actionDelegate onTapVideoCall];
    }
}

- (void)audioCall
{
    [self.inputView closeKeyboard];
    if ([self.inputView.actionDelegate respondsToSelector:@selector(onTapAudioCall)]) {
        [self.inputView.actionDelegate onTapAudioCall];
    }
}

- (void)topicEvent:(UIButton *)btn
{
    [self.inputView closeKeyboard];
    if ([self.inputView.actionDelegate respondsToSelector:@selector(onTapTopicButton:)]) {
        [self.inputView.actionDelegate onTapTopicButton:btn];
    }
}

- (void)actionSheet
{
    [self.popupByWindow showWithAnimated:YES];
}

- (void)inviteAction
{
    if (self.popupByWindow.isShowing) {
        [self.popupByWindow hideWithAnimated:NO];
    }
}

- (void)dealloc
{
    //[[NSNotificationCenter defaultCenter] removeObserver:self name:kInvitorVideoOrAudioNotiKey object:nil];
}


- (CGFloat)extraHeight
{
    return 42;
}

- (CGSize)sizeThatFits:(CGSize)size
{
    CGSize superSize = [super sizeThatFits:size];
    return CGSizeMake(SCREEN_WIDTH, superSize.height + [self extraHeight]);
}

- (void)willChangeHeight:(CGFloat)height
{
    height = MAX(height, 32);
    [super willChangeHeight:height];
}

- (void)didChangeHeight:(CGFloat)height
{
    height = MAX(height, 32);
    [super didChangeHeight:height];
}

/// 状态发生变化
- (void)refreshStatus:(HLIMInputStatus)status
{
    [super refreshStatus:status];
    
//    if (status == IMInputStatusAudio) {
//        self.sendButton.hidden = YES;
//        self.audioRecognizeButton.hidden = NO;
//    } else {
//        self.sendButton.hidden = NO;
//        self.audioRecognizeButton.hidden = YES;
//    }
}

- (void)updatePriceWithVideo:(NSString *)vPrice audio:(NSString *)aPrice
{
    
    NSString *audioTitle = [NSString stringWithFormat:@"语音%@金币/分钟", aPrice];
    NSString *videoTitle = [NSString stringWithFormat:@"视频%@金币/分钟", vPrice];
    if ([aPrice isEqualToString:@"0"] && [vPrice isEqualToString:@"0"]) {
        audioTitle = @"语音";
        videoTitle = @"视频";
    }
    
    MJWeakSelf;
    self.popupByWindow.items = @[
                                 [QMUIPopupMenuItem itemWithImage:UIImageMake(@"im_input_bar_audio") title:[NSString stringWithFormat:@"  %@", audioTitle] handler:^(__kindof QMUIPopupMenuItem * _Nonnull aItem, __kindof UIControl<QMUIPopupMenuItemViewProtocol> * _Nonnull aItemView, NSInteger section, NSInteger index) {
                                     [weakSelf audioCall];
                                     [aItem.menuView hideWithAnimated:YES];
                                 }],
                                 [QMUIPopupMenuItem itemWithImage:UIImageMake(@"im_input_bar_video") title:[NSString stringWithFormat:@"  %@", videoTitle] handler:^(__kindof QMUIPopupMenuItem * _Nonnull aItem, __kindof UIControl<QMUIPopupMenuItemViewProtocol> * _Nonnull aItemView, NSInteger section, NSInteger index) {
                                     [weakSelf videoCall];
                                     [aItem.menuView hideWithAnimated:YES];
                                 }]];
}

#pragma mamrk - <私有>

/// 创建自定义的输入框按钮
- (UIButton *)barButton:(UIImage *)image
{
    QMUIButton *barButton = [[QMUIButton alloc] init];
    [barButton setImage:image forState:UIControlStateNormal];
    barButton.contentEdgeInsets = UIEdgeInsetsMake(6, 6, 6, 6);
    return barButton;
}


@end
